package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuSaleAttrValueMapper;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * spu销售属性值表service层实现类
 */
@Service
public class SpuSaleAttrValueServiceImpl implements SpuSaleAttrValueService {

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    /**
     * 保存数据
     *
     * @param spuInfoId
     * @param spuSaleAttrValueList
     */
    @Override
    public void save(Long spuInfoId, List<SpuSaleAttrValue> spuSaleAttrValueList,String saleAttrName) {
        //遍历
        spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
            //补全数据
            spuSaleAttrValue.setSpuId(spuInfoId);
            spuSaleAttrValue.setSaleAttrName(saleAttrName);
            //新增数据
            int insert = spuSaleAttrValueMapper.insert(spuSaleAttrValue);
            if(insert<= 0){
                throw new GmallException("新增spu销售属性值表数据失败",null);
            }
        });
    }
}
